********************************** Master file ****************************************
**                                                                                   **
**          Replicating tables and graphics for the paper "Trading Favors?"          **
**                                                                                   **
**             NOTE:  Replication requires installation of the following             **
**              user-written commands, available for download from the               **
**           Boston College Statistical Software Components (SSC) archive:           **
**                                                                                   **
**                                 egenmore                                          **
**                                  estout                                           **
**                                  xtpqml                                           **
**                                                                                   **
***************************************************************************************


*The following data file must be placed in the same directory as this do file

use "TradingFavors_v2.dta", clear



************************************** Table 1 *******************************************
eststo clear
foreach var in unsc birthregionD incumbentregD {
xi: qui eststo: xtpqml wb_start `var'  i.year, irr fe i(countrycode)
xi: qui eststo: xtpqml wb_start `var'  i.year, irr fe i(geoname_id)
}

esttab  using lin.tex, eform nocon title(Number of projects - main effects\label{main}) ///
mtitles("UNSC" "UNSC" "Birth Region" "Birth Region" "Coethnic Region" "Coethnic Region" ) ///
replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) scalars("N_g Groups")  ///
ren(birthregionD MainEffect  unsc MainEffect  coethnicshare MainEffect ///
    incumbentregD MainEffect) nogap compress obslast nonotes
	
eststo clear
foreach var in unsc birthregionD incumbentregD {

xi: qui eststo: xtpqml wb_commit `var'  i.year, irr fe i(countrycode)
xi: qui eststo: xtpqml wb_commit `var'  i.year, irr fe i(geoname_id)

}

esttab  using mainC.tex, eform nocon title(Commitments - main effects\label{mainC}) ///
mtitles("UNSC" "UNSC" "Birth Region" "Birth Region" "Coethnic Region" "Coethnic Region" )  ///
replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) scalars("N_g Groups")  ///
ren(birthregionD MainEffect unsc MainEffect  coethnicshare MainEffect ///
    incumbentregD MainEffect)  nogap compress obslast nonotes

************************************** Table 2 *******************************************

eststo clear
foreach var in birthregionD incumbentregD  {

gen interaction=`var'*unsc
xi: qui eststo: xtpqml wb_start unsc `var' interaction i.year, irr fe i(geoname_id)
xi: qui eststo: xtpqml wb_commit unsc `var' interaction i.year, irr fe i(geoname_id)

drop interaction
}

esttab using interC.tex, eform nocon title(UNSC membership and political connections - Interaction effects\label{inter}) ///
mtitles("Birth Region" "Coethnic Region" "Coethnic Share" ) scalars("N_g Regions") ///
replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) nogap compress ///
ren(birthregionD BirthRegion interaction UNSCInteraction ///
    unsc UNSC  incumbentregD CoethnicRegion) obslast nonotes ///
    order(UNSC BirthRegion CoethnicRegion) ///
    addnotes("The table reports Incidence Rate Ratios from FE Poisson regressions. The dependent" ///
        "variable is USD commitments to projects started in each region and year. All regressions" ///
        "include region and year fixed effects. Standard errors are cluster-robust at the country level." ///
        "P-values under the coefficients.")

************************************** Placebo *******************************************
gen uny=year if unsc==1
bysort country: egen maxuny=max(uny)
bysort country: egen minuny=min(uny)


eststo clear
foreach i of numlist -5/5{
    gen placebo2=(year==minuny+`i' | year==minuny+`i'+1)
    gen placebo=placebo2*birthregionD
xi: qui eststo: xtpqml wb_start placebo  i.year, irr fe i(geoname_id)
*xi: qui eststo: xtpoisson wb_start placebo i.year , fe vce(r)  
drop placebo*
}
esttab using nbirth.csv, replace cells(b ci) keep(placebo) plain

eststo clear
foreach i of numlist -5/5{
    gen placebo2=(year==minuny+`i')
    gen placebo=placebo2*incumbentregD
xi: qui eststo: xtpoisson wb_start placebo i.year , fe vce(r)  
drop placebo*
}
esttab using ncoeth.csv, replace cells(b ci) keep(placebo) plain

************************************** Table 3 *******************************************

eststo clear
foreach var in birthregionD incumbentregD  {

gen interaction=`var'*unsc
xi: qui eststo: xtpqml IDA_start unsc `var' interaction i.year , irr fe i(geoname_id)
xi: qui eststo: xtpqml IDA_commit unsc `var' interaction i.year , irr fe i(geoname_id)
xi: qui eststo: xtpqml IBRD_start unsc `var' interaction i.year , irr fe i(geoname_id)
xi: qui eststo: xtpqml IBRD_commit unsc `var' interaction i.year , irr fe i(geoname_id)
drop interaction
}

* I don't manage to make reg 2 and 4 compute with birthregion (fewer obs?)
esttab est1 est2 est5 est6 est3 est4 est7 est8 using IDAIBRD.tex, eform nocon title(IDA vs IBRD\label{IDAIBRD}) ///
mtitles("Birth Region" "Coethnic Region" ) ///
scalars("p F-test" "N_g Regions" ) ///
replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) nogap compress ///
ren(birthregionD BirthRegion interaction UNSCInteraction ///
    unsc UNSC  incumbentregD CoethnicRegion) obslast nonotes ///
    order(UNSC BirthRegion CoethnicRegion) ///
    addnotes("The table reports Incidence Rate Ratios from FE Poisson regressions. The dependent" ///
        "variable is the number of World Bank projects started in each region and year. All regressions" ///
        "include region and year fixed effects. Standard errors are cluster-robust at the country level." ///
        "p-values under the coefficients." ///
        "P-values at the bottom of columns (4) to (6) refer to differences from columns (1) to (3).")


************************************** Table 4 *******************************************
* Dum1 is an indicator coded manually on the basis of the eccel sheet "UNSC_voting_records_2015_8-CSH.xlsx"
* described in Dreher et al. (2018) and generously shared by the authors 

eststo clear
foreach var in birthregionD incumbentregD  {

gen interaction=`var'*unsc
gen inter1=interaction*Dum1
xi: qui eststo: xtpqml wb_start unsc `var' inter* i.year, irr fe i(geoname_id)
xi: qui eststo: xtpqml wb_commit unsc `var' inter* i.year, irr fe i(geoname_id)

drop inter*
}

esttab  using US3.tex, eform nocon title(Voting alignment\label{US3}) ///
mtitles("Projects" "Committments" "Projects" "Committments"  ) ///
scalars("N_g Regions" ) ///
replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) nogap compress ///
ren(birthregionD BirthRegion interaction UNSCInteraction ///
    unsc UNSC  inter1 XAllAgreement inter2  XShareAgreement ///
    incumbentregD CoethnicRegion) obslast nonotes ///
    order(UNSC BirthRegion CoethnicRegion UNSCInteraction) ///
    addnotes("The table reports Incidence Rate Ratios from FE Poisson regressions. The dependent" ///
        "variable is number of and USD commitments to projects started in each region and year." ///
        "All regressions include region and year fixed effects. Standard errors are cluster-robust" ///
        "at the country level. P-values under the coefficients.")

************************************** Table 5 *******************************************
* Create dummy for rich or poor regions in terms of NTL within country

egen NTL_median=median(NTL_sum) if year==1995, by(country)
gen rich=NTL_sum>=NTL_median
replace rich=. if NTL_median==. | NTL_sum==.
egen temp=max(rich), by(geoname_id)
replace rich=temp
drop temp

eststo clear
foreach var in birthregionD incumbentregD  {

gen interaction=`var'*unsc
gen inter2=unsc*rich
xi: qui eststo: xtpqml wb_start unsc `var' rich inter* i.year, irr fe i(geoname_id)
xi: qui eststo: xtpqml wb_commit unsc `var' rich inter* i.year, irr fe i(geoname_id)
drop inter*
}
esttab est1 est3 est2 est4 using rich2.tex, eform nocon title(Local income (1995 nighttime lights)\label{rich2}) ///
mtitles("Birth Region" "Coethnic Region" "Birth Region" "Coethnic Region") scalars("N_g Regions") ///
replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) nogap compress ///
ren(birthregionD ConnectedRegion interaction UNSCInteraction ///
    unsc UNSC  inter1 ConnectedXRich inter3 ConnectedXRichXUNSC ///
    incumbentregD ConnectedRegion inter2 UNSCXRich ) obslast nonotes ///
    order(UNSC BirthRegion CoethnicRegion)

************************************** Figures A1-A4 *******************************************

preserve
keep  if birthregionD ==1
keep countrycode geoname_id year Africa
bysort countrycode: gen total=_N
egen changes=nvals(geoname_id), by(countrycode)
gen turnover=changes/total
collapse turn Africa, by(countrycode)
label var Africa Africa
kdensity turn if Africa, addplot(kdensity turn if Africa == 0) legend(ring(0) pos(2) label(1 "Africa") label(2 "Out of Africa") ) ti(Birth region turnover over the period)  xti("")
graph save turnover.png, replace
restore

preserve
keep  if incumbentregD ==1
keep countrycode geoname_id year Africa
bysort countrycode: gen total=_N
egen changes=nvals(geoname_id), by(countrycode)
gen turnover=changes/total
collapse turn Africa, by(countrycode)
kdensity turn if Africa, addplot(kdensity turn if Africa == 0) legend(ring(0) pos(2) label(1 "Africa") label(2 "Out of Africa") ) ti(Ethnic region turnover over the period)  xti("")
graph save turnover2.png, replace
restore

preserve
keep  if birthregionD ==1
keep countrycode geoname_id year demo
bysort countrycode: gen total=_N
egen changes=nvals(geoname_id), by(countrycode)
gen turnover=changes/total
collapse turn demo, by(countrycode)
label var demo Democracies
kdensity turn if demo, addplot(kdensity turn if demo == 0) legend(ring(0) pos(2) label(1 "Democracies") label(2 "Autocracies") ) ti(Birth region turnover over the period)  xti("")
graph save turnoverdem.png, replace
restore

preserve
keep  if incumbentregD ==1
keep countrycode geoname_id year demo
bysort countrycode: gen total=_N
egen changes=nvals(geoname_id), by(countrycode)
gen turnover=changes/total
collapse turn demo, by(countrycode)
kdensity turn if demo, addplot(kdensity turn if demo == 0) legend(ring(0) pos(2) label(1 "Democracies") label(2 "Autocracies") ) ti(Ethnic region turnover over the period)  xti("")
graph save turnoverdem2.png, replace
restore

************************************** Table A1 *******************************************

tab country  if Dum1 

************************************** Figures A5-A6 *******************************************

preserve
collapse (mean) meanbreg= birthregionD (sd) sdbreg=birthregionD (count) nbreg=birthregionD, by(rich)
generate hibreg = meanbreg + invttail(n-1,0.025)*(sdbreg / sqrt(n))
generate lobreg = meanbreg - invttail(n-1,0.025)*(sdbreg / sqrt(n))
graph twoway (bar meanbreg rich, barw(0.85)) (rcap hibreg lobreg rich), xlabel( 0 "NTL < mean in 1995" 1 "NTL > mean in 1995", noticks) xtitle("") ytitle("Freq. birth region") legend(off)
restore

preserve
collapse (mean) meanincreg= incumbentregD (sd) sdincreg=incumbentregD (count) nincreg=incumbentregD, by(rich)
generate hiincreg = meanincreg + invttail(n-1,0.025)*(sdincreg / sqrt(n))
generate loincreg = meanincreg - invttail(n-1,0.025)*(sdincreg / sqrt(n))
graph twoway (bar meanincreg rich, barw(0.85)) (rcap hiincreg loincreg rich), xlabel( 0 "NTL < mean in 1995" 1 "NTL > mean in 1995", noticks) xtitle("") ytitle("Freq. coethnic region") legend(off)
restore

************************************** Table A2 *******************************************

eststo clear
foreach var in incumbentregD  {
gen interaction=`var'*unsc
gen inter2=interaction*Africa
xi: qui xtpqml wb_start unsc `var' inter* Africa i.year , irr fe i(geoname_id)
test inter2
scalar diffpN= r(p) 
xi: qui xtpqml wb_commit unsc `var' inter* Africa i.year , irr fe i(geoname_id)
test inter2
scalar diffpC= r(p) 
xi: qui eststo: xtpqml wb_start unsc `var' interaction i.year if Africa==1, irr fe i(geoname_id)
estadd scalar diffp = diffpC
xi: qui eststo: xtpqml wb_commit unsc `var' interaction i.year if Africa==1, irr fe i(geoname_id)
estadd scalar diffp = diffpN
xi: qui eststo: xtpqml wb_start unsc `var' interaction i.year if Africa==0, irr fe i(geoname_id)
xi: qui eststo: xtpqml wb_commit unsc `var' interaction i.year if Africa==0, irr fe i(geoname_id)
drop inter*
}

esttab using ethnicAfrica.tex, eform nocon title(UNSC membership in coethnic regions - Africa subsample\label{ethnicAfrica}) ///
scalars("diffp DiffP-val"  "N_g Regions" ) ///
replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) nogap compress ///
ren(interaction UNSCInteraction unsc UNSC   ///
    incumbentregD CoethnicRegion) obslast nonotes ///
    order(UNSC CoethnicRegion )  mti("N"  "USD" "N"  "USD") ///
    addnotes("The table reports Incidence Rate Ratios from FE Poisson regressions. The dependent" ///
        "variable is the number of World Bank projects started in each region and year. All regressions" ///
        "include region and year fixed effects. Standard errors are cluster-robust at the country level." ///
        "p-values under the coefficients." ///
        "P-values at the bottom of columns (4) to (6) refer to differences from columns (1) to (3).")

************************************** Table A3 *******************************************
eststo clear		
foreach var in wb_start wb_commit {
xi: qui eststo: xtpqml `var' unsc birthregionD  i.year if Africa==1, irr fe i(countrycode)
xi: qui eststo: xtpqml `var' unsc birthregionD  i.year if Africa==1, irr fe i(geoname_id)
xi: qui eststo: xtpqml `var' unsc birthregionD  i.year if Africa==0, irr fe i(countrycode)
xi: qui eststo: xtpqml `var' unsc birthregionD  i.year if Africa==0, irr fe i(geoname_id)
}

esttab using interAfrica.tex, eform nocon title(Commitments - Africa\label{interAfrica}) ///
nomtitles scalars("N_g Countries") replace drop(_I*) cells(b(fmt(3)) p(fmt(3) par([ ]))) ///
ren(unsc UNSC birthregionD BirthRegion) nogap compress obslast nonote ///
addnotes("The table reports Incidence Rate Ratios from FE Poisson regressions. The dependent" ///
        "variable is number of and USD commitments to projects started in each region and year. All regressions" ///
        "include country and year fixed effects. Standard errors are cluster-robust at the country level." ///
        "P-values under the coefficients.")




